from sqlalchemy.orm import Session

from apis_module.jsonvo.user_vo import UserInfoUpdateRequest
from apis_module.model.users_model import Users


async def get_user_info(student_id: str, db: Session) -> Users:
    """
    根据用户名查询要登录系统的用户
    :param db:
    :param student_id:
    :return:
    """
    user: Users = db.query(Users).filter(Users.student_so == student_id).distinct().first()

    return user


class UserDao:
    @classmethod
    def update_user_info(cls, info_model: UserInfoUpdateRequest, db: Session) -> Users:
        user: Users = db.query(Users).filter(Users.user_id == info_model.user_id).first()

        if user is not None:
            try:
                user.nickname = info_model.nickname
                user.avatar = info_model.avatar
                db.merge(user)
                db.commit()

                return user
            except Exception as e:
                db.rollback()
                raise e

        return None
